/**
 * Created by chocolate on 16/4/28.
 */
'use strict'

import React, {
	Component,
	View,
	Text,
	StyleSheet,
}from 'react-native';

var loginCheckQuery = require('./Query').query;
var AppNav = require('./AppNav');
var UsrLogin = require('./Login');
class TransitView extends Component{

	constructor(props){
		super(props);
		this.state = {
			isLonginChecking:true,
			shouldShowLogin:false,
		};
	}

	componentWillMount(){
		storage.load({
			key: 'user',
			autoSync: true,
			syncInBackground: true
		}).then(ret => {
			loginCheckQuery.loginCheckQuery(ret.phone,ret.password,this,function(owner,response){
				console.log(response);
				if(owner)
				{
					if(response.success !== true ){
						owner.setState({shouldShowLogin:true});
						console.log('账户验证失败，跳转到登录界面，需要用户手动填写账号和密码进行登录');
					}
					else{
						owner.setState({shouldShowLogin:false});
						global.g_user = response.message;
						console.log('账户验证成功，直接切换到主界面！用户手机号为：'+g_user.phone);
					}
					owner.setState({isLonginChecking:false});
				}
			})
		}).catch(err => {
			console.log('无法从本地读取到用户账号信息，可能第一次使用，也可能是进行了清除缓存操作。切换到用户登录界面');
			this.setState({shouldShowLogin:true});
			this.setState({isLonginChecking:false});
		})
	}

	
	render(){
		if(this.state.isLonginChecking)
		{
			return(
				<View style={styles.container}>
					<Text style={styles.text}>正在加载中...</Text>
				</View>); //此处可以添加过场图片
		}
		else{
			if(this.state.shouldShowLogin){return(<UsrLogin appnav={AppNav}/>);}
			else{return(<AppNav/>);}
		}
	}
}

const styles = StyleSheet.create({
	container:{
		backgroundColor:'#fff',
		alignItems:'center',
		justifyContent:'center',
		flex:1
	},
	text:{
		fontSize:20,
		color:'#00F',
		textAlign:'center',
	}
})

module.exports = TransitView;

